﻿#include "function_detectionElectrode.h"
//OpenCV MatתHalcon HObject

DetectionElectrode::DetectionElectrode(QDialog* parent, QSettings* qsettings, QString section) : QDialog(parent)
{
    m_qsettings = qsettings;
    m_section = section;

    //m_model_image_path = QCoreApplication::applicationDirPath() + "/" + m_model_image_path;

    this->setWindowTitle(("电极检测"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    setFixedSize(1920*ratio_x,1010*ratio_y);

    m_view = new MyQGraphicsView(this);
    m_view->setGeometry(660 * ratio_x, 0, 1000 * 1.25 * ratio_x, 800 * 1.25 * ratio_y);

    QSize control_size(120*ratio_x,40*ratio_y);

    QWidget* p = new QWidget(this);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(160 * ratio_x, 40 * ratio_y);

  

    m_qlabel_row1_offset = new QLabel();
    m_qlabel_row1_offset->setText("y左缩进");
    m_qlabel_row1_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_row1_offset, index, 0);
    m_qspinbox_row1_offset = new QSpinBox();
    m_qspinbox_row1_offset->setFixedSize(control_size);
    m_qspinbox_row1_offset->setMinimum(0);
    m_qspinbox_row1_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_row1_offset, index, 1);
    
    index++;
    m_qlabel_row2_offset = new QLabel();
    m_qlabel_row2_offset->setText("y右缩进");
    m_qlabel_row2_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_row2_offset, index, 0);
    m_qspinbox_row2_offset = new QSpinBox();
    m_qspinbox_row2_offset->setFixedSize(control_size);
    m_qspinbox_row2_offset->setMinimum(0);
    m_qspinbox_row2_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_row2_offset, index, 1);
    
    index++;
    m_qlabel_column1_offset = new QLabel();
    m_qlabel_column1_offset->setText("x左缩进");
    m_qlabel_column1_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column1_offset,index,0);
    m_qspinbox_column1_offset = new QSpinBox();
    m_qspinbox_column1_offset->setFixedSize(control_size);
    m_qspinbox_column1_offset->setMinimum(0);
    m_qspinbox_column1_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column1_offset, index, 1);


    index++;
    m_qlabel_column2_offset = new QLabel();
    m_qlabel_column2_offset->setText("x右缩进");
    m_qlabel_column2_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column2_offset,index,0);
    m_qspinbox_column2_offset = new QSpinBox();
    m_qspinbox_column2_offset->setFixedSize(control_size);
    m_qspinbox_column2_offset->setMinimum(0);
    m_qspinbox_column2_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column2_offset, index, 1);

    index++;
    m_qlabel_column21_offset = new QLabel();
    m_qlabel_column21_offset->setText("中间左缩进");
    m_qlabel_column21_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column21_offset, index, 0);
    m_qspinbox_column21_offset = new QSpinBox();
    m_qspinbox_column21_offset->setFixedSize(control_size);
    m_qspinbox_column21_offset->setMinimum(0);
    m_qspinbox_column21_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column21_offset, index, 1);


    index++;
    m_qlabel_column22_offset = new QLabel();
    m_qlabel_column22_offset->setText("中间右缩进");
    m_qlabel_column22_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column22_offset, index, 0);
    m_qspinbox_column22_offset = new QSpinBox();
    m_qspinbox_column22_offset->setFixedSize(control_size);
    m_qspinbox_column22_offset->setMinimum(0);
    m_qspinbox_column22_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column22_offset, index, 1);

    index++;
    m_qlabel_height_offset = new QLabel();
    m_qlabel_height_offset->setText("高度缩进");
    m_qlabel_height_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_height_offset,index,0);
    m_qspinbox_height_offset = new QSpinBox();
    m_qspinbox_height_offset->setFixedSize(control_size);
    m_qspinbox_height_offset->setMinimum(0);
    m_qspinbox_height_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_height_offset, index, 1);

    index++;
    m_qlabel_area_unfilled_threshold = new QLabel();
    m_qlabel_area_unfilled_threshold->setText("缺角面积阈值");
    m_qlabel_area_unfilled_threshold->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_area_unfilled_threshold, index, 0);
    m_qspinbox_area_unfilled_threshold = new QSpinBox();
    m_qspinbox_area_unfilled_threshold->setFixedSize(control_size);
    m_qspinbox_area_unfilled_threshold->setMinimum(0);
    m_qspinbox_area_unfilled_threshold->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_area_unfilled_threshold, index, 1);
    
    index++;
    m_qlabel_area_defect_threshold = new QLabel();
    m_qlabel_area_defect_threshold->setText("缺陷面积阈值");
    m_qlabel_area_defect_threshold->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_area_defect_threshold, index, 0);
    m_qspinbox_area_defect_threshold = new QSpinBox();
    m_qspinbox_area_defect_threshold->setFixedSize(control_size);
    m_qspinbox_area_defect_threshold->setMinimum(0);
    m_qspinbox_area_defect_threshold->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_area_defect_threshold, index, 1);
    index++;

    m_qlabel_gray_threshold = new QLabel();
    m_qlabel_gray_threshold->setText("缺陷灰度阈值");
    m_qlabel_gray_threshold->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_gray_threshold, index, 0);
    index++;
    m_qspinbox_gray_threshold = new QSpinBox();
    m_qspinbox_gray_threshold->setFixedSize(control_size);
    m_qspinbox_gray_threshold->setMinimum(1);
    m_qspinbox_gray_threshold->setMaximum(254);
    m_qspinbox_gray_threshold->setReadOnly(true);
    qgridLayout->addWidget(m_qspinbox_gray_threshold, index, 0);
    m_qslider_gray_threshold = new QSlider(Qt::Horizontal);
    m_qslider_gray_threshold->setTickPosition(QSlider::NoTicks);
    m_qslider_gray_threshold->setSingleStep(1);
    m_qslider_gray_threshold->setMinimum(1);
    m_qslider_gray_threshold->setMaximum(254);
    qgridLayout->addWidget(m_qslider_gray_threshold,index,1);
    index++;


    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(control_size);
    m_pushbutton_save_parameter->setText("保存参数");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &DetectionElectrode::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);

    index++;
    m_pushbutton_detection = new QPushButton(this);
    m_pushbutton_detection->setFixedSize(control_size);
    m_pushbutton_detection->setText("检测");
    connect(m_pushbutton_detection, &QPushButton::clicked, this, &DetectionElectrode::detectionImage);
    qgridLayout->addWidget(m_pushbutton_detection, index,0);
    
    index++;
    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(control_size);
    m_pushbutton_return->setText("退出");
    qgridLayout->addWidget(m_pushbutton_return, index, 0);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });

    
    try
    {
        m_proc_detection = new HDevProcedure("detectionElectrode");
        m_call_detection = new HDevProcedureCall(*m_proc_detection);
    }
    catch (HDevEngineException& hdev_exception)
    {
        //QMessageBox::information(nullptr, "", hdev_exception.Message());
    }


    loadParameter();
    connect(m_qslider_gray_threshold, &QSlider::valueChanged, this, [=](int value) {
            m_qspinbox_gray_threshold->setValue(value);
            saveParameter();
            testImage();
    });
}

DetectionElectrode::~DetectionElectrode()
{
    ;
}


bool DetectionElectrode::detection( HObject image_in, QString& message)
{
  /*  if (m_image.empty())
    {
        message += "检测图像为空 ";
        return false;
    }*/
    try
    {
        // Set the input parameters of the procedure
        if (!region.IsInitialized())
        {
            return false;
        }
        m_call_detection->SetInputIconicParamObject("image", image_in);
        m_call_detection->SetInputIconicParamObject("region", region);

        m_call_detection->SetInputCtrlParamTuple("row1_offset", row1_offset);
        m_call_detection->SetInputCtrlParamTuple("column1_offset", column1_offset);
        m_call_detection->SetInputCtrlParamTuple("row2_offset", row2_offset);
        m_call_detection->SetInputCtrlParamTuple("column2_offset", column2_offset);
        m_call_detection->SetInputCtrlParamTuple("column21_offset", column21_offset);
        m_call_detection->SetInputCtrlParamTuple("column22_offset", column22_offset);

        m_call_detection->SetInputCtrlParamTuple("height_offset", height_offset);
        m_call_detection->SetInputCtrlParamTuple("area_unfilled_threshold", area_unfilled_threshold);

        m_call_detection->SetInputCtrlParamTuple("area_defect_threshold", area_defect_threshold);
        m_call_detection->SetInputCtrlParamTuple("gray_defect_threshold", gray_defect_threshold);




       

        // Execute the procedure

        //HDevEngine().StartDebugServer();
        //m_call_detection->SetWaitForDebugConnection(true);
        m_call_detection->Execute();
        //HDevEngine().StopDebugServer();

        region_roi= m_call_detection->GetOutputIconicParamObject("region_roi");
        region_unfilled = m_call_detection->GetOutputIconicParamObject("region_unfilled");
        region_defect = m_call_detection->GetOutputIconicParamObject("region_defect");

        
        // get the output parameters
        HTuple ret = m_call_detection->GetOutputCtrlParamTuple(1);
        HTuple msg = m_call_detection->GetOutputCtrlParamTuple(2);
        HTuple htuple_area_unfilled = m_call_detection->GetOutputCtrlParamTuple(3);
        HTuple htuple_gray_defect = m_call_detection->GetOutputCtrlParamTuple(4);
        m_result = ret[0].I();
        if (m_result == 1)
        {
            message += msg[0].S();
            return true;
        }
        else
        {
            message += msg[0].S();
            return false;
        }
    }
    catch (HDevEngineException& hdev_exception)
    {
        message = hdev_exception.Message();
        return false;
    }
  
    return true;
}


void DetectionElectrode::detectionImage()
{
    if(m_image.empty())
    {
        //QMessageBox::information(nullptr,"","没有图像");
        return;
    }
    //region = parent()->m_function_blob_match->region_out;
    QString message;
    detection(image_in,message);

    Mat image_show = himageToMat(image_in);
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);
    m_view->setImage(q_image, message);
}

void DetectionElectrode::draw(Mat image_in,Mat& image_out)
{
    if(image_in.empty())
        return;
    image_out = drawRegion(image_in, region_roi, Scalar(0, 255, 0));
    image_out = drawRegion(image_out, region_unfilled, Scalar(0, 0, 255));
    image_out = drawRegion(image_out, region_defect, Scalar(0, 0, 255));
    GenEmptyRegion(&region_roi);
    GenEmptyRegion(&region_unfilled);
    GenEmptyRegion(&region_defect);
}

void DetectionElectrode::loadParameter()
{
    row1_offset = m_qsettings->value(m_section + "/" + "row1_offset").toInt();
    column1_offset = m_qsettings->value(m_section + "/" + "column1_offset").toInt();
    row2_offset = m_qsettings->value(m_section + "/" + "row2_offset").toInt();
    column2_offset = m_qsettings->value(m_section + "/" + "column2_offset").toInt();
    column21_offset = m_qsettings->value(m_section + "/" + "column21_offset").toInt();
    column22_offset = m_qsettings->value(m_section + "/" + "column22_offset").toInt();
    height_offset = m_qsettings->value(m_section + "/" + "height_offset").toInt();
    area_unfilled_threshold = m_qsettings->value(m_section + "/" + "area_unfilled_threshold").toInt();
    area_defect_threshold = m_qsettings->value(m_section + "/" + "area_defect_threshold").toInt();
    gray_defect_threshold = m_qsettings->value(m_section + "/" + "gray_defect_threshold").toInt();

    m_qspinbox_row1_offset->setValue(row1_offset);
    m_qspinbox_column1_offset->setValue(column1_offset);
    m_qspinbox_row2_offset->setValue(row2_offset);
    m_qspinbox_column2_offset->setValue(column2_offset);

    m_qspinbox_column21_offset->setValue(column21_offset);
    m_qspinbox_column22_offset->setValue(column22_offset);
    m_qspinbox_height_offset->setValue(height_offset);
    m_qspinbox_area_unfilled_threshold->setValue(area_unfilled_threshold);
    m_qspinbox_area_defect_threshold->setValue(area_defect_threshold);
    m_qspinbox_gray_threshold->setValue(gray_defect_threshold);

    return ;
}

void DetectionElectrode::saveParameter()
{
    row1_offset = m_qspinbox_row1_offset->value();
    column1_offset = m_qspinbox_column1_offset->value();
    row2_offset = m_qspinbox_row2_offset->value();
    column2_offset = m_qspinbox_column2_offset->value();
    column21_offset = m_qspinbox_column21_offset->value();
    column22_offset = m_qspinbox_column22_offset->value();
    height_offset = m_qspinbox_height_offset->value();
    area_unfilled_threshold = m_qspinbox_area_unfilled_threshold->value();
    area_defect_threshold = m_qspinbox_area_defect_threshold->value();
    gray_defect_threshold = m_qspinbox_gray_threshold->value();

    m_qsettings->setValue(m_section + "/" + "row1_offset", row1_offset);
    m_qsettings->setValue(m_section + "/" + "column1_offset", column1_offset);
    m_qsettings->setValue(m_section + "/" + "row2_offset", row2_offset);
    m_qsettings->setValue(m_section + "/" + "column2_offset", column2_offset);
    m_qsettings->setValue(m_section + "/" + "column21_offset", column21_offset);
    m_qsettings->setValue(m_section + "/" + "column22_offset", column22_offset);
    m_qsettings->setValue(m_section + "/" + "height_offset", height_offset);
    m_qsettings->setValue(m_section + "/" + "area_unfilled_threshold", area_unfilled_threshold);
    m_qsettings->setValue(m_section + "/" + "area_defect_threshold", area_defect_threshold);
    m_qsettings->setValue(m_section + "/" + "gray_defect_threshold", gray_defect_threshold);
}

bool DetectionElectrode::showDialog()
{
    return true;
}

void DetectionElectrode::testImage()
{
    QString message;
    detection(image_in,message);

    Mat image_show = himageToMat(image_in);
    image_show  = Mat::zeros(Size(image_show.cols,image_show.rows),CV_8UC3);
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);
    m_view->setImage(q_image, message);
}

